package com.cn.onetrip.activity;

import java.util.List;

import com.cn.onetrip.adapter.VideosItemAdapter;
import com.cn.onetrip.objects.VideosMediaObj;
import com.cn.onetrip.activity.R;
import com.cn.onetrip.untility.SysApplication;
import com.cn.onetrip.untility.Untility;
import com.cn.onetrip.view.AdView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class VideosActivity extends Activity implements OnItemClickListener {
	// public static View view;
	private ListView videoList;
	private VideosItemAdapter adapter;
	private Button quit;
	private List<VideosMediaObj> videosMediaList;
	private AdView adView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Window w = getWindow();
		// view = w.getDecorView();
		setContentView(R.layout.activity_video);
		videoList = (ListView) findViewById(R.id.VideoList);
		adapter = new VideosItemAdapter(VideosActivity.this, videoList, handler);
		videoList.setAdapter(adapter);
		videoList.setOnItemClickListener(this);
		quit = (Button) findViewById(R.id.quit);
		quit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// view.startAnimation(Untility.rQuest1Animation);
				// GroupActivity.changeFirstView();
				finish();
				overridePendingTransition(R.anim.push_left_translate,
						R.anim.pull_right_translate);
			}
		});
		
		adView = (AdView) findViewById(R.id.ad_view);
	}

	@Override
	protected void onResume() {
		loadDate();
		adView.startTimer();
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
		adapter.clean();
		adapter.threadQuit();
		adView.stopTimer();
	}

	public void loadDate() {
		adapter.clean();
		videosMediaList = SysApplication.videoList;
		if (videosMediaList.size() == 1) {
			return;
		} else if (videosMediaList.size() == 2) {

		} else {
			for (int i = 1; i < videosMediaList.size(); i++) {
				VideosMediaObj videoObj = videosMediaList.get(i);
				adapter.addVideos(videoObj.cover, videoObj.name,
						videoObj.duration);
			}
		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		intent.setClass(getApplicationContext(), ScenicVideoPlayActivity.class);
		bundle.putString("videoUrl", videosMediaList.get(arg2 + 1).mediaUrl);
		intent.putExtras(bundle);
		startActivity(intent);
		overridePendingTransition(R.anim.push_right_translate,
				R.anim.pull_left_translate);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			overridePendingTransition(R.anim.push_left_translate,
					R.anim.pull_right_translate);
		}
		return super.onKeyDown(keyCode, event);
	}

	public Handler handler = new Handler() {
		public void dispatchMessage(android.os.Message msg) {
			if (msg.what == 0) {
			}
		};
	};
}
